﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class ProductDetail : PageBase
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null) return "";
            else return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            action = Request.QueryString["action"].ToString();
            Databang();
            if (action == "add")
            {
                lblTitle.Text = "添加商品";                
            }
            else if (action == "edit")
            {
                lblTitle.Text = "编辑商品";

                InitData();
            }
            else
            {
                lblTitle.Text = "查看商品";
                Add_Submit.Visible = false;
                InitData();
                
            }
            
        }
        
     }

    protected void InitData()
    {
        ProductBLL BllProduct = new ProductBLL();
        string ID = Request.QueryString["ID"].ToString();
        T_PRODUCT Product = BllProduct.GetProductByID(ID);
        Code.Text = Product.CODE;
        Name.Text = Product.NAME;
       // Model.Text = Product.MODEL;
       // Standard.Text = Product.STANDARD;
       // Size.Text = Product.SIZE;
       // Origin.Text = Product.ORIGIN;
       // Brand.Text = Product.BRAND;
        ListItem Item= Unit.Items.FindByValue(Product.UNIT_ID.ToString());
        Unit.SelectedIndex = Unit.Items.IndexOf(Item);
        Item = Weight.Items.FindByValue(Product.WEIGHT_ID.ToString());
        Weight.SelectedIndex = Weight.Items.IndexOf(Item);

    }

    protected void Databang()
    {
        UnitBLL BllUnit = new UnitBLL();
        BllUnit.AddEqualCondition("Type", (int)Enumerations.UnitType.计件单位);
        Unit.DataSource = BllUnit.GetUnitList();       
        Unit.DataBind();
        BllUnit.ClearWhereCondition();
        BllUnit.AddEqualCondition("Type", (int)Enumerations.UnitType.计量单位);
        Weight.DataSource = BllUnit.GetUnitList();
        Weight.DataBind();


    }
    protected void Add_Submit_Click(object sender, EventArgs e)
    {
        ProductBLL BllProduct = new ProductBLL();
        if (action == "add")
        {

            T_PRODUCT Product = new T_PRODUCT();
            Product.CODE = Code.Text.Trim();
            Product.NAME = Name.Text.Trim();
            ////Product.MODEL = Model.Text.Trim();
            //Product.STANDARD = Standard.Text.Trim();
            //Product.SIZE = Size.Text.Trim();
            //Product.ORIGIN = Origin.Text.Trim();
            //Product.BRAND = Brand.Text.Trim();
            if (Unit.Items.Count > 0)
                Product.UNIT_ID = new Guid(Unit.SelectedValue);
            else 
                Product.UNIT_ID = null;
            if (Weight.Items.Count > 0)
                Product.WEIGHT_ID = new Guid(Weight.SelectedValue);
            else
                Product.WEIGHT_ID = null;
            if (BllProduct.AddProduct(Product)<1)
                {
                    ShowMessage("添加失败");
                }
                else
                {
                    Response.Redirect("ProductManager.aspx");
                }
        }
        else
        {
            string ID = Request.QueryString["ID"].ToString();
            T_PRODUCT Product = new T_PRODUCT();
            Product.ID = new Guid(ID);
            Product.CODE = Code.Text.Trim();
            Product.NAME = Name.Text.Trim();
            //Product.MODEL = Model.Text.Trim();
            //Product.STANDARD = Standard.Text.Trim();
            //Product.SIZE = Size.Text.Trim();
            //Product.ORIGIN = Origin.Text.Trim();
            //Product.BRAND = Brand.Text.Trim();
            if (Unit.Items.Count > 0)
                Product.UNIT_ID = new Guid(Unit.SelectedValue);
            else
                Product.UNIT_ID = null;
            if (Weight.Items.Count > 0)
                Product.WEIGHT_ID = new Guid(Weight.SelectedValue);
            else
                Product.WEIGHT_ID = null;
            if (BllProduct.UpdateProduct(Product) < 0)
            {
                ShowMessage("修改失败");
            }
            else
            {
                Response.Redirect("ProductManager.aspx");
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductManager.aspx");
    }
}
